package com.cx.web.exception;

import com.cx.meta.exception.CxException;
import com.cx.meta.exception.ErrorResponse;
import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author flysand
 * @package com.cx.web.exception
 * @date 2021-11-07 18:34
 * @des CxExceptionHandler:全局异常处理  处理所有500自定义异常
 */
@ControllerAdvice
public class CxExceptionHandler {

    private final static Logger logger = LoggerFactory.getLogger(CxExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(CxException.class)
    @ResponseStatus(HttpStatus.OK)
    public ErrorResponse handleCxException(CxException e){
        ErrorResponse response = new ErrorResponse();
        logger.error(e.getMessage() + ":"+ Throwables.getStackTraceAsString(e));
        response.setCode(e.getCode());
        response.setMessage(e.getMessage());
        return response;
    }

}
